<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit();
}

/**
 * Mileage
**/
class Mileage_stampCard extends Legacy_BlockProcedure
{
	/**
	 * @var Mileage_MileHandler
	 * 
	 * @private
	**/
	var $_mHandlerMile = null;

	/**
	 * @var Mileage_RecHandler
	 * 
	 * @private
	**/
	var $_mHandlerRec = null;

	/**
	 * @var Mileage_MileObject
	 * 
	 * @private
	**/
	var $_mObjectMile = null;

	/**
	 * @var string[]
	 * 
	 * @private
	**/
	var $_mOptions = array();

	/**
	 * @var object
	 * 
	 * @private
	**/
	var $_actionForm = NULL;

	/**
	 * prepare
	 * 
	 * @param   void
	 * 
	 * @return  bool
	 * 
	 * @public
	**/
	function prepare()
	{
		return parent::prepare() && $this->_parseOptions() && $this->_setupObject();
	}
	
	/**
	 * _parseOptions
	 * 
	 * @param   void
	 * 
	 * @return  bool
	 * 
	 * @private
	**/
	function _parseOptions()
	{
		$opts = explode('|', $this->_mBlock->get('options'));
		$this->_mOptions = array(
			'group'           => (isset($opts[0]) && intval($opts[0]) > 0 ? intval($opts[0]) : 0),
			'except_login'    => (isset($opts[1]) && intval($opts[1]) > 0 ? intval($opts[1]) : 0),
			'except_ticket'   => (isset($opts[2]) && intval($opts[2]) > 0 ? intval($opts[2]) : 0),
			'except_register' => (isset($opts[3]) && intval($opts[3]) > 0 ? intval($opts[3]) : 0),
		);

		return true;
	}
	
	/**
	 * getBlockOption
	 * 
	 * @param   string  $key
	 * 
	 * @return  string
	 * 
	 * @public
	**/
	function getBlockOption($key)
	{
		return isset($this->_mOptions[$key]) ? $this->_mOptions[$key] : null;
	}
	
	/**
	 * getOptionForm
	 * 
	 * @param   void
	 * 
	 * @return  string
	 * 
	 * @public
	**/
	function getOptionForm()
	{
		if (!$this->prepare()) {
			return null;
		}

		$cattree = Mileage_Utils::getXcat($this->_mBlock->get('dirname'));

		$present_group   = intval($this->getBlockOption('group'));
		$except_login    = intval($this->getBlockOption('except_login'));
		$except_ticket   = intval($this->getBlockOption('except_ticket'));
		$except_register = intval($this->getBlockOption('except_register'));

		// FORM
		$form = _AD_MILEAGE_LANG_GROUP . ' : ';
		$form.= '<input type="radio" name="options[0]" value="0" ' . (($present_group == 0) ? "checked='checked'": "") . ' />' . _ALL;
		if (!empty($cattree)) {
			foreach ($cattree as $k => $v) {
				$checked = ($present_group == $v['cat_id']) ? "checked='checked'": "";
				$form .= "&nbsp;<input type='radio' name='options[0]' value='". intval($v['cat_id']) ."' $checked />";
				$form .= htmlspecialchars($v['cat_title'], ENT_QUOTES, _CHARSET) . "\n";
			}
		}

		$form .= '<br />' . _AD_MILEAGE_LANG_EXCEPT_LOGIN . ' : ';
		$form .= '<input type="radio" name="options[1]" value="1" ' . (($except_login == 1) ? "checked='checked'": '') . ' />' . _YES;
		$form .= '<input type="radio" name="options[1]" value="0" ' . (($except_login == 0) ? "checked='checked'": '') . ' />' . _NO;

		$form .= '<br />' . _AD_MILEAGE_LANG_EXCEPT_TICKET . ' : ';
		$form .= '<input type="radio" name="options[2]" value="1" ' . (($except_ticket == 1) ? "checked='checked'": '') . ' />' . _YES;
		$form .= '<input type="radio" name="options[2]" value="0" ' . (($except_ticket == 0) ? "checked='checked'": '') . ' />' . _NO;

		$form .= '<br />' . _AD_MILEAGE_LANG_EXCEPT_REGISTER . ' : ';
		$form .= '<input type="radio" name="options[3]" value="1" ' . (($except_register == 1) ? "checked='checked'": '') . ' />' . _YES;
		$form .= '<input type="radio" name="options[3]" value="0" ' . (($except_register == 0) ? "checked='checked'": '') . ' />' . _NO;

		return $form;
	}

	/**
	 * _setupObject
	 * 
	 * @param   void
	 * 
	 * @return  bool
	 * 
	 * @private
	**/
	function _setupObject()
	{
		$root =& XCube_Root::getSingleton();
		$uid = 0;
		if (is_object($root->mContext->mXoopsUser)) {
			$uid = $root->mContext->mXoopsUser->get('uid');
		} else {
			return FALSE;
		}

		$dirname = $this->_mBlock->get('dirname');

		//get module asset for handlers
		$asset = null;
		XCube_DelegateUtils::call(
			'Module.mileage.Global.Event.GetAssetManager',
			new XCube_Ref($asset),
			$dirname
		);

		$present_group   = intval($this->getBlockOption('group'));
		$except_login    = intval($this->getBlockOption('except_login'));
		$except_ticket   = intval($this->getBlockOption('except_ticket'));
		$except_register = intval($this->getBlockOption('except_register'));

		// MILE
		$_mHandlerMile =& $asset->getObject('handler', 'mile');
		$mileCriteria = new CriteriaCompo('1', '1');
		$mileCriteria->add(new Criteria('`url`', 'charge', '!='));
		if ($present_group > 0) {
			$mileCriteria->add(new Criteria('`group`', '%[' . $present_group . ']%', 'LIKE'));
		}
		if ($except_login == 1) {
			$mileCriteria->add(new Criteria('`url`', 'login', '!='));
		}
		if ($except_register == 1) {
			$mileCriteria->add(new Criteria('`url`', 'register', '!='));
		}
		$mileCriteria->add(new Criteria('`url`', 'remittance%', 'NOT LIKE'));
		$mileCriteria->add(new Criteria('`url`', 'ticket%', 'NOT LIKE'));
		if ($except_ticket == 0) {
			// user group
			$memberHandler =& Mileage_Utils::getXoopsHandler('member');
			$groupsByUid = $memberHandler->getGroupsByUser($uid);

			$mileCriteria->add(new Criteria('`url`', 'ticket:'), 'OR');
			$mileCriteria->add(new Criteria('`url`', 'ticket%u'. intval($uid) .'|%', 'LIKE'), 'OR');
			foreach ($groupsByUid as $group) {
				$mileCriteria->add(new Criteria('`url`', 'ticket%g'. intval($group) .'|%', 'LIKE'), 'OR');
			}
		}
		$objectMile =& $_mHandlerMile->getObjects($mileCriteria);

		// REC
		$_mHandlerRec =& $asset->getObject('handler', 'rec');

		for ($i = 0; $i < count($objectMile); $i++) {
			$mile_id = $objectMile[$i]->get('mile_id');
			$recCriteria = new CriteriaCompo('1', '1');
			$recCriteria->add(new Criteria('uid', $uid));
			$recCriteria->add(new Criteria('mile_id', $mile_id));
			$objectRec = & $_mHandlerRec->getObjects($recCriteria);

			$total_points = 0;
			for ($j = 0; $j < count($objectRec); $j++) {
				$total_points = $total_points + $objectRec[$j]->get('point');
			}
			$objectMile[$i]->mile_number = count($objectRec);
			$objectMile[$i]->total_points = intval($total_points);
		}

		$this->_mObjectMile = $objectMile;

		return TRUE;
	}

	/**
	 * execute
	 * 
	 * @param   void
	 * 
	 * @return  void
	 * 
	 * @public
	**/
	function execute()
	{
		$root =& XCube_Root::getSingleton();

		$dirname = $this->_mBlock->get('dirname');

		$render =& $this->getRenderTarget();
		$render->setTemplateName($this->_mBlock->get('template'));

		$render->setAttribute('mile', $this->_mObjectMile);
		$render->setAttribute('dirname', $dirname);
	//	$render->setAttribute('lang', Mileage_Utils::getConstantLang($dirname));// NOT USE
		$render->setAttribute('isMobile', Mileage_Utils::checkMobile());

		$renderSystem =& $root->getRenderSystem($this->getRenderSystemName());
		$renderSystem->renderBlock($render);
	}

}
